package com.nd.mobile.study.view.helper;

import android.content.Context;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.nd.mobile.study.R;

public class Validator {

	private static final String	PATTERN_USERNAME		= ".{1,50}";
	private static final String	PATTERN_PASSWORD		= ".{6,16}";
	private static final String	PATTERN_MOBILE_NUMBER	= "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
	private static final String	PATTERN_EMAIL			= "\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
	private static final String	PATTERN_REALNAME		= "[\\u4e00-\\u9fa5]{2,10}";
	private Context				mContext;
	private Animation			mAlertAni;

	public Validator(Context context) {
		mContext = context;
		mAlertAni = AnimationUtils.loadAnimation(mContext, R.anim.shake);
	}

	private boolean validate(TextView input, String pattern, int alertRes) {
		if (null != input && input.getText().toString().trim().matches(pattern)) {
			return true;
		} else {
			alert(input, alertRes);
			return false;
		}
	}

	private void alert(TextView input, int alertRes) {
		ToastHelper.displayToastLong(mContext, alertRes);
		input.requestFocus();
		input.startAnimation(mAlertAni);
	}
	

	/**
	 * 验证输入的邮箱格式是否有效
	 * 
	 * @param email
	 *            邮箱地址
	 * @return 有效返回true，否则返回false
	 */
	public boolean validateEmail(TextView email) {
		return validate(email, PATTERN_EMAIL, R.string.validator_alert_email);
	}

	/**
	 * 验证输入的手机号码是否有效
	 * 
	 * @param mobileNumber
	 *            手机号码
	 * @return 有效返回true;否则返回false
	 */
	public boolean validateMobileNumber(TextView mobileNumber) {
		return validate(mobileNumber, PATTERN_MOBILE_NUMBER, R.string.validator_alert_mobile_number);
	}

	/**
	 * 验证输入的密码是否有效
	 * 
	 * @param password
	 *            密码
	 * @return 有效返回true;否则返回false
	 */
	public boolean validatePassword(TextView password) {
		return validate(password, PATTERN_PASSWORD, R.string.validator_alert_password);
	}

	/**
	 * 验证两次密码是否一致
	 * 
	 * @param password
	 *            第一次输入密码
	 * @param comfirmedPassword
	 *            第二次输入密码
	 * @return 一致返回true;否则返回false
	 */
	public boolean validateConfirmPassword(TextView password, TextView comfirmedPassword) {
		if (null != comfirmedPassword && comfirmedPassword.getText().toString().equals(password.getText().toString())) {
			return true;
		} else {
			alert(comfirmedPassword, R.string.validator_alert_password_confirm);
			return false;
		}
	}

	/**
	 * 验证姓名长度是否有效
	 * 
	 * @param realName
	 *            姓名
	 * @return 有效返回true;否则返回false
	 */
	public boolean validateRealName(TextView realName) {
		return validate(realName, PATTERN_REALNAME, R.string.validator_alert_real_name);
	}

	/**
	 * 验证登录帐号长度是否有效
	 * 
	 * @param userName
	 *            登录帐号
	 * @return 有效返回true;否则返回false
	 */
	public boolean validateUserName(TextView userName) {
		return validate(userName, PATTERN_USERNAME, R.string.validator_alert_username);
	}
}
